;
(function() {
  window.vueMessageList = new Vue({
    el: '#wx_message_config',
    data: {
      mesModel:'',
      permissions: JSON.parse(window.localStorage.getItem('permissions')),
      info: {},
      users: [],
      id: hlUrlUtil.GetQueryString('id'),
      configFlag: false, // 修改配置
      nodeUser: [],

      resourceFlag:false,

      projectList:[],
      selectedPro:[],

      buildingList:[],
      selectedBuild:[],

      resourcesList:[],
      haveResources:[],
      usersId:'',
    },
    filters: {
      formatDate: function(item) {
        return hlDateUtil.formatUTCDate(item)
      },
      formatUndefined: function(item) {
        if (item == undefined) return '--'
        return item
      }
    },
    computed:{
      allFlag(){
        if(!this.resourcesList.length) return false
        let flag = true
        this.resourcesList.map(item =>{
          if(!item.isSelect){
            flag = false
          }
        })
        return flag
      },
      haveResourcesIds(){
        let arr = []
        this.haveResources.map(item =>{
          arr.push(item.unitId)
        })
        return arr
      }
    },
    methods: {
      // 获取项目详情
      getProject(){
        hlhttp.get({
			    url: 'user/projects',
			    success: (res) => {
            this.projectList = hlArrayUtil.replaceData(res, [{key: 'projectName', rKey: 'name'}, {key: 'projectId', rKey: 'code'}]);
			    }
			  })
      },
      // 资源权限弹出层开启(获取项目资源)
      showResourceFun(id){
        this.resourceFlag = true
        this.selectedPro = []
        this.selectedBuild = []
        this.resourcesList = []
        this.usersId = id
        let data = {
          template_id:this.id,
          user_id:id,
          page:'1',
          limit:'9999'
        }
        hlhttp.get({
          url: 'weixin/message_resource_privileges',
          data: data,
			    success: (res) => {
            if(!res.datas || !res.datas.length){
              this.haveResourcesIds = []
              this.haveResources = []
            }else{
              this.haveResources = res.datas
            } 
			    }
			  })
      },
      // 选择项目时触发
      selectProFun(){
        this.selectedBuild = []
        this.resourcesList = []
        let data = {
          project_id:this.selectedPro + '',
          grouping_id:'#',
        }
        hlhttp.get({
          url: 'project/grouping/_building',
          data: data,
			    success: (res) => {
            this.buildingList = hlArrayUtil.replaceData(res, [{key: 'buildingId', rKey: 'code'}]);
			    }
			  })
      },
      // 选择楼栋是触发
      selectBuildFun(){
        let data = {
          project_id:this.selectedPro + '',
          grouping_id:'#',
          building_id:this.selectedBuild + '',
          floor_id:'#',
          page:'1',
          unit_type:'#',
          page_size:'9999'
        }
        hlhttp.get({
          url: 'bu_rent_units/_search',
          data: data,
			    success: (res) => {
            this.setLeftList(res.units)
			    }
			  })
      },
      // 处理左边选中未选中数据
      setLeftList(res){
        res.map(item =>{
          item.unitId = item.id
          if(this.haveResourcesIds.indexOf(item.id) != -1){
            item.isSelect = true
          }else{
            item.isSelect = false
          }
        })
        this.resourcesList = res
      },  
      // 选中资源（单元）
      chooseResources(index){
        this.resourcesList[index].isSelect = !this.resourcesList[index].isSelect
        this.setResources()
      },
      // 选中全部资源（单元）
      chooseAllResources(){
        let flag = this.allFlag
        this.resourcesList.map(item =>{
          item.isSelect = flag?false:true
        })
        this.setResources()
      },
      // 设置已选资源
      setResources(){
        this.resourcesList.map(item =>{
          let index = this.haveResourcesIds.indexOf(item.unitId)
          if(item.isSelect){
            if(index == -1){
              this.haveResources.push(item)
            }
          }else{
            if(index != -1){
              this.haveResources.splice(index,1)
            }
          }
        })
      },
      // 删除已选资源
      delResourcesItem(index){
        this.haveResources.splice(index,1)
        this.setLeftList(this.resourcesList)
      },
      // 确认添加资源
      sureAddResources(){
        let arr = []
        this.haveResources.map(item =>{
          arr.push(item.unitId)
        })
        let data = {
          template_id:this.id,
          user_id:this.usersId,
          unit_ids:arr.length?arr + '' : '#'
        }
        hlhttp.post({
          url: 'weixin/message_resource_privilege',
          data: data,
			    success: (res) => {
            hl.toast('添加成功')
            this.resourceFlag = false
			    }
			  })
      },
      // 获取微信公众号消息模板信息
      getTemplatesInfo: function() {
        var self = this;
        hlhttp.get({
          url: 'wxpn_message_templates/info',
          data: { wxpn_message_template_id: self.id},
          success: function(data) {
            self.info = data.info;
            if(data.users){
              self.users = data.users;
              approve.data.selectPerson = data.users
              self.nodeUser = data.users
            }
            // 分割消息模版
            var mesArr = data.info.format.split('|')
            var mesStr = mesArr.join('<br/>')
            self.mesModel = mesStr
          }
        })
      },
      // 删除人员
      delperson (i) {
        this.nodeUser.splice(i, 1)
        approve.data.selectPerson = this.nodeUser;
        approve.dom.showRight()
      },
      // 停启用模版
      changeValid: function(valid) {
        if (valid === 'Y') {
          this.info.valid = 'Y'
        }
        if (valid === 'N') {
          this.info.valid = 'N'
        }
      },
      // 无法配置员工时 保存配置调用
      saveConfig: function() {
        // 消息模板类型为对内员工 且 消息模版分类为账单
        if (this.info.type === 'I' && this.info.category === 'BL') {
          this.saveConfigMember()
        } else {
          this.saveNoConfigMember()
        }
      },
      // 无法配置员工时 保存配置调用
      saveNoConfigMember: function() {
        var self = this;
        hlhttp.put({
          url: 'wxpn_message_template/valid',
          data: {
            wxpn_message_template_id: self.id,
            opt: self.info.valid
          },
          success: function(data) {
            hl.toast('保存成功！')
          }
        })
      },
      // 配置员工时 保存配置调用
      saveConfigMember: function() {
        var self = this;
        var usersId = []
        self.users.map(function(_item) {
          usersId.push(_item.id)
        })
        if (self.users.length) {  usersId = usersId.join() }
        if (!self.users.length) { usersId = '#'}

        hlhttp.post({
          url: 'wxpn_message_template/config',
          data: {
            wxpn_message_template_id: self.id,
            valid: self.info.valid,
            users: usersId
          },
          success: function(data) {
            hl.toast('保存成功！')
          }
        })
      },
      configMember: function() {
        // window.sessionStorage.setItem('WX_MESSAGE_TEMPLATE_USER', JSON.stringify(this.users))
        //
        // configMember.getOrganizationNodeData(0, 1, $('.assignees-wrapper'), false)
        // configMember.checkMember()
        $('.config-member-pop-container').show()
        $('.backdrop').show()
      },
      saveSelect () {
        this.users = JSON.parse(JSON.stringify(this.nodeUser))
        this.hideSelectPerson()
      },
      hideSelectPerson () {
        $('.config-member-pop-container').hide()
        $('.backdrop').hide()
      }
    },
    mounted: function() {
      this.getProject()
      approve.dom.addinitialDom()
      approve.event.wxddNodeEvent((data) => {
        console.log(data);

        this.nodeUser = data;
      })
      this.getTemplatesInfo()
    }
  })
}())
